package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.mapper.YddCateMapper;
import com.itheima.ydd.pojo.YddArticle;
import com.itheima.ydd.pojo.YddCate;
import com.itheima.ydd.service.YddArticleService;
import com.itheima.ydd.service.YddCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddCateServiceImpl extends ServiceImpl<YddCateMapper, YddCate> implements YddCateService {
    @Autowired
    private YddArticleService yddArticleService;

    /**
     * 带条件分页查询
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ResponseEntity<R<PageInfo<YddCate>>> findPageOrName(String catename, String page, String limit) {
        IPage<YddCate> pg = new Page<YddCate>(Integer.parseInt(page),Integer.parseInt(limit));
        LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(catename != null, YddCate::getCatename, catename);
        wrapper.orderByDesc(YddCate::getSortNum);
        page(pg, wrapper);
        PageInfo<YddCate> pageInfo=new PageInfo<>();
        pageInfo.setCount((int) pg.getTotal());
        pageInfo.setTotalPages((int) pg.getPages());
        pageInfo.setPageSize((int) pg.getPages());
        pageInfo.setCurrentPage((int) pg.getCurrent());
        pageInfo.setData(pg.getRecords());
        return ResponseEntity.ok(R.success(pageInfo));
    }

    /**
     * 根据分类id删除,需要判断ydd_article下面cateid有没有数据,有的话要先删除
     *
     * @param id
     */
    @Override
    public ResponseEntity<R> removeWishYddArticle(String id) {
        //查询当前分类id下有没有数据
        LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddCate::getId, id);

        //解除两表关联
        //先查询ydd_article
        LambdaQueryWrapper<YddArticle> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(YddArticle::getCateid,id);
        int count = yddArticleService.count(articleWrapper);
        //int count = yddArticleService.count();
        if(count!=0){
            return ResponseEntity.ok(R.err("分类下有文章无法删除"));
        }
        //删除分类
        removeById(id);
        return ResponseEntity.ok(R.success(null));
    }

    /**
     * 添加分类
     * @param yddCate
     * @return
     */
    @Override
    public ResponseEntity<R> saveYddCate(YddCate yddCate) {
        //查询添加的分类id是否已存在,如已存在则不能添加
        LambdaQueryWrapper<YddCate> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(YddCate::getCatename,yddCate.getCatename());
        YddCate ydd = getOne(wrapper);
        if(ObjectUtils.isNotEmpty(ydd)){
            return ResponseEntity.ok(R.err("已存在此分类,无法添加"));
        }
        save(yddCate);

        return ResponseEntity.ok(R.success(null));
    }

    /**
     * 修改功能
     * @param yddCate
     * @return
     */
    @Override
    public ResponseEntity<R> updateYddCate(YddCate yddCate) {
        //判断修改的分类名称是否已存在
        LambdaQueryWrapper<YddCate> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(YddCate::getCatename,yddCate.getCatename());
        updateById(yddCate);
        return ResponseEntity.ok(R.success(null));
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public ResponseEntity<R<List<YddCate>>> findAll() {
        List<YddCate> list = list();
        return ResponseEntity.ok(R.success(list));
    }


}
